#! /bin/sh
# PCP QA Test No. 102
# pmlc parser/scanner/error handling
#
# Copyright (c) 1995-2002 Silicon Graphics, Inc.  All Rights Reserved.
#

seq=`basename $0`
echo "QA output created by $seq"

# get standard filters
. ./common.product
. ./common.filter
. ./common.check

_cleanup()
{
    _restore_config $PCP_SYSCONFIG_DIR/pmcd
    _restore_primary_logger
    _service pmcd restart 2>&1 | _filter_pcp_restart
    _wait_for_pmcd
    _restore_auto_restart pmcd
    _service pmlogger restart 2>&1 | _filter_pcp_restart
    _wait_for_pmlogger
    _restore_auto_restart pmlogger
    $sudo rm -f $tmp $tmp.*
    exit $status
}

status=1
trap "_cleanup" 0 1 2 3 15

_stop_auto_restart pmcd
_stop_auto_restart pmlogger

_filter_pmlc()
{
    # error message mapping is due to differences between PCP_PLATFORMs
    #
    sed \
	-e 's/connect [0-9][0-9]*/connect PID/' \
	-e 's/Version [0-9].*/Version VERSION/' \
	-e 's/^parse error/Syntax error/' \
	-e 's/line [0-9]*/line NUM/g' \
	-e 's/^syntax error/Syntax error/' \
	-e 's/^Reason: Unknown or illegal.*$/Reason: No PMCD agent for domain of request/'
}

# if we're below the PMAPI (as opposed to sleeping in the main pmlogger
# loop) when SIGTERM is delivered, we risk seeing errors like
#    pmGetInDom(29.2): IPC protocol failure
# and pmlogger will then exit ... treat these like the expected Info:
# ... Caught signal ... message
#
_my_logger_filter()
{
    sed -e '/^_*pm.*: IPC protocol failure/s/.*/[DATE] pmlogger(PID) Info: pmlogger: Caught signal 15, exiting/'
}

_disable_agent_restart()
{
    echo PMCD_RESTART_AGENTS=0 > $tmp.defaults
    $sudo cp $tmp.defaults $PCP_SYSCONFIG_DIR/pmcd
}

signal=$PCP_BINADM_DIR/pmsignal

# real QA test starts here
_save_config $PCP_SYSCONFIG_DIR/pmcd
_writable_primary_logger
_disable_agent_restart
ls -l $PCP_VAR_DIR/config/pmlogger/config.default >>$seq_full
cat $PCP_VAR_DIR/config/pmlogger/config.default >>$seq_full
pmlc -P </dev/null 2>&1 >>$seq_full
if ! _service pmcd restart 2>&1; then _exit 1; fi \
| _filter_pcp_restart
_wait_for_pmcd || _exit 1
if ! _service pmlogger restart 2>&1; then _exit 1; fi \
| _filter_pcp_restart
_wait_for_pmlogger || _exit 1
$PCP_PS_PROG $PCP_PS_ALL_FLAGS | grep -E '[P]PID|/[p]mlogger( |$)' >>$seq_full

cat <<End-of-File >$tmp.pmlc
# not connected
query sample.drift
advisory on 1 hour sample.drift

connect primary
# incomplete - syntax error
query sample.bin [
query {
sample.drift
# eat input
# up to
# next } -- curly brace
}
query {
barf.no.metric
# suck to } ... not that one, this one ..
}

End-of-File
_try_pmlc
_filter_pmlc <$tmp.err
_filter_pmlc <$tmp.out

for delta in "-1 msec" "75 hours" "599 hours" "1200 hours"
do
    $sudo rm -f $tmp.*		# refactor-ok
    cat <<End-of-File >$tmp.config
log advisory on $delta sample.bin [100]
End-of-File
    $sudo rm -f $tmp.*		# refactor-ok
    _start_up_pmlogger -L -c /dev/null -l $tmp.log -T 6 $tmp
    pmsleep 1.5
    cat <<End-of-File >$tmp.pmlc
connect $pid
log advisory on $delta sample.bin [100]
End-of-File
    _try_pmlc
    _filter_pmlc <$tmp.err
    _filter_pmlc <$tmp.out
    $sudo $signal -s TERM $pid
    _wait_pmlogger_end $pid || _exit 1
    _filter_pmlogger_log <$tmp.log | _my_logger_filter
done
pmsleep 1

cat <<End-of-File >$tmp.pmlc
connect primary
log mandatory on 1 hour { sample.bin, sample.drift, sample.seconds }
query sample.bin[333]
End-of-File
_try_pmlc
_filter_pmlc <$tmp.err
_filter_pmlc <$tmp.out

$sudo $signal -a pmdasample
pmsleep 1				# time to cleanup
pminfo -f sample >/dev/null 2>&1	# and make sure pmcd notices

cat <<End-of-File >$tmp.pmlc
connect primary
query sample.bin[100]
query sample.bin[100]
End-of-File
_try_pmlc
_filter_pmlc <$tmp.err
_filter_pmlc <$tmp.out

$sudo $signal -a -s HUP pmcd
pmsleep 1				# time to cleanup
cat <<End-of-File >$tmp.pmlc
connect primary
query sample.bin[100]

query { sample.bin,sample.drift sample.seconds }
query { sample.bin sample.drift,sample.seconds }
query sample.bin [100,200,300 400 500,700]
query sample.bin [100,"bin-200"]
End-of-File
_try_pmlc
_filter_pmlc <$tmp.err
# nl state is non-deterministic because pmlogger might, or might not,
# have logger these metrics by now
#
_filter_pmlc <$tmp.out \
| sed -e 's/mand on    /mand on  nl/'

cat <<End-of-File >$tmp.pmlc
connect primary
log mandatory maybe {
    sample.bin
    sample.drift, sample.seconds
}
End-of-File
_try_pmlc
_filter_pmlc <$tmp.err
_filter_pmlc <$tmp.out

status=0
exit
